package cn.itcast.web.controller.cargo;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/cargo/export", name = "报运单模块")
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/contractList", name = "已上报合同信息")
    public String contractList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        // 查询对象
        ContractExample example = new ContractExample();
        // 封装条件
        example.createCriteria().andCompanyIdEqualTo(getCompanyId()).andStateEqualTo(1);
        // 排序
        example.setOrderByClause("create_time desc");

        PageInfo page = contractService.findByPage(pageNum, pageSize, example);
        request.setAttribute("page", page);

        return "cargo/export/export-contractList";
    }

    @Reference
    private ExportService exportService;

    @RequestMapping(value = "/list", name = "报运单信息")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andCompanyIdEqualTo(getCompanyId());

        PageInfo page = exportService.findByPage(pageNum, pageSize, exportExample);
        request.setAttribute("page", page);
        return "cargo/export/export-list";
    }

    /*
        前端提交：
            id:1
            id:2
        后端接收：
            String [] id = {"1","2"}
            String id ="1,2"
     */
    @RequestMapping(value = "/toExport", name = "跳转报运单新增页面")
    public String toExport(String id) {
        request.setAttribute("id", id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping(value = "/edit", name = "新增或修改")
    public String edit(Export export) {

        if (StrUtil.hasEmpty(export.getId())) { // 新增报运单
            export.setCompanyId(getCompanyId());
            export.setCompanyName(getCompanyName());
            exportService.save(export);
        }else{ //更新报运单
            exportService.update(export);
        }



        return "redirect:/cargo/export/list.do";
    }

    @Reference
    private ExportProductService exportProductService;

    @RequestMapping(value = "/toUpdate", name = "跳转报运单修改页面")
    public String toUpdate(String id) {
        // 1.根据报运单id查询报运单对象
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        // 2.根据报运单id查询商品列表
        ExportProductExample epExample = new ExportProductExample();
        epExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> eps = exportProductService.findAll(epExample);
        request.setAttribute("eps", eps);

        return "cargo/export/export-update";
    }


    @RequestMapping("/exportE")
    public String exportE(String id){
        // 1.电子报运
        exportService.exportE(id);

        // 2.根据报运单id，查询报运结果
        exportService.changeExportEResult(id);

        return "redirect:/cargo/export/list.do";
    }
}
